using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Zhaoxi.MSACommerce.StockService.Core.Entities;

namespace Zhaoxi.MSACommerce.StockService.Infrastructure;

public class StockDbContext(DbContextOptions<StockDbContext> options) : DbContext(options)
{
    // 设计时构造函数 - 使用 this() 调用主构造函数
    public StockDbContext() : this(new DbContextOptions<StockDbContext>())
    {
    }
    public DbSet<SkuStock> SkuStocks => Set<SkuStock>();
    
    public DbSet<StockResv> StockResvs => Set<StockResv>();

  
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base. OnModelCreating(builder);
        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // 设计时配置 - 只有在没有配置选项时才使用
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseMySql("server=192.168.2.178;user=root;password=RootPassword123!;database=tb_stock", new MySqlServerVersion(new Version(8, 0, 0)));
        }
    }
}